#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/init.h> /* Needed for the macros */
#include <linux/kernel.h> /* Needed for pr_info() */
#include <linux/module.h> /* Needed by all modules */
#include <linux/platform_device.h>
#include <linux/input.h>

/**
 *
 *
 my_platform_device003 {
 status = "okay";
 compatible ="my_platform_device_003";
 interrupt-parent = <&gpio1>;
 interrupts = <7 2>;
 };

 how  to test:
 tree  /sys/input/
 sudo  hexdumop  /sys/input/eventX


 */

static struct input_dev *button_dev;

 bool a = true;

static irqreturn_t button_interrupt(int irq, void *dummy) {
	a=!a;
	pr_info("input_report_key %d  \n", a);
	input_report_key(button_dev, BTN_0, a);
	input_sync(button_dev);
	pr_info("button_interrupt start \n");
	return IRQ_HANDLED;
}

unsigned int num;

int myprobe(struct platform_device *pdev) {
	int ret;

	struct resource *res;
	pr_info("myplatformdriver myprobe \n");

	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	pr_err("irq : res->start :%llu\n", res->start);
	num = res->start;
	pr_info("irq_num:%d\n", num);
	ret = request_irq(num, button_interrupt, res->flags, "my_key_driver",
	NULL);

	if (ret < 0) {
		pr_err("request_irq  failed \n");
		return ret;
	}

	button_dev = input_allocate_device();
	if (!button_dev) {
		printk(KERN_ERR "button.c: Not enough memory\n");
		ret = -ENOMEM;
		goto err_free_irq;
	}
	button_dev->name = "my simple key2";
	set_bit(EV_KEY, button_dev->evbit);
	set_bit(BTN_0, button_dev->keybit);

//	button_dev->evbit[0] = BIT_MASK(EV_KEY);
//	button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);
	button_dev->dev.parent = &pdev->dev;
	ret = input_register_device(button_dev);
	if (ret) {
		printk(KERN_ERR "button.c: Failed to register device\n");
		goto err_free_dev;
	}

	return 0;
	err_free_dev: input_free_device(button_dev);
	err_free_irq: free_irq(num, NULL);
	return ret;
}

int myremove(struct platform_device *pdev) {
	free_irq(num, NULL);
	input_free_device(button_dev);
	pr_info("myplatformdriver myremove \n");
	return 0;
}

int mysuspend(struct device *pdev, pm_message_t state) {
	pr_info("myplatformdriver mysuspend \n");
	return 0;
}

int myresume(struct device *pdev) {
	pr_info("myplatformdriver myresume \n");
	return 0;
}

struct of_device_id my_of_match_table =
		{ .compatible = "my_platform_device_003", };

struct platform_driver my_platform_driver = { .driver = { .of_match_table =
		&my_of_match_table, .name = "my-platform-driver", .owner = THIS_MODULE,
		.suspend = mysuspend, .resume = myresume, }, .probe = myprobe, .remove =
		myremove, };

module_platform_driver(my_platform_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");
